package com.hengtiansoft.lfy.utils;

import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;

/**
 * Created by Administrator on 2017/2/16.
 */

public class PopUtil {
    public interface MyClickListener{
        public void copy(int pos,View v);
        public void delete(int pos,View v);
        public void collection(int pos,View v);
    }
    public static PopupWindow  showTipPopupWindow(String[] showStr,boolean left,final View anchorView, final MyClickListener myClickListener, final int pos) {
        final View contentView = LayoutInflater.from(anchorView.getContext()).inflate(R.layout.popuw_content_top_arrow_layout, null);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 创建PopupWindow时候指定高宽时showAsDropDown能够自适应
        // 如果设置为wrap_content,showAsDropDown会认为下面空间一直很充足（我以认为这个Google的bug）
        // 备注如果PopupWindow里面有ListView,ScrollView时，一定要动态设置PopupWindow的大小
        final PopupWindow popupWindow = new PopupWindow(contentView,
                contentView.getMeasuredWidth(), contentView.getMeasuredHeight(), false);
        if(showStr.length==3){
            ((TextView)contentView.findViewById(R.id.bv_copy)).setText(showStr[0]);
            ((TextView)contentView.findViewById(R.id.bv_delete)).setText(showStr[1]);
            ((TextView)contentView.findViewById(R.id.bv_collection)).setText(showStr[2]);
        }else if(showStr.length==2){
            ((TextView)contentView.findViewById(R.id.bv_delete)).setText(showStr[0]);
            ((TextView)contentView.findViewById(R.id.bv_collection)).setText(showStr[1]);
            ((TextView)contentView.findViewById(R.id.bv_copy)).setVisibility(View.GONE);
        }else if(showStr.length==1){
            ((TextView)contentView.findViewById(R.id.bv_delete)).setText(showStr[0]);
            ((TextView)contentView.findViewById(R.id.bv_copy)).setVisibility(View.GONE);
            ((TextView)contentView.findViewById(R.id.bv_collection)).setVisibility(View.GONE);
        }
        (contentView.findViewById(R.id.bv_delete)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                myClickListener.delete(pos,view);
            }
        });
        (contentView.findViewById(R.id.bv_copy)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                myClickListener.copy(pos,view);
            }
        });
        (contentView.findViewById(R.id.bv_collection)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                myClickListener.collection(pos,view);
            }
        });

        // 如果不设置PopupWindow的背景，有些版本就会出现一个问题：无论是点击外部区域还是Back键都无法dismiss弹框
        popupWindow.setBackgroundDrawable(new ColorDrawable());

        // setOutsideTouchable设置生效的前提是setTouchable(true)和setFocusable(false)
        popupWindow.setOutsideTouchable(true);

        // 设置为true之后，PopupWindow内容区域 才可以响应点击事件
        popupWindow.setTouchable(true);

        // true时，点击返回键先消失 PopupWindow
        // 但是设置为true时setOutsideTouchable，setTouchable方法就失效了（点击外部不消失，内容区域也不响应事件）
        // false时PopupWindow不处理返回键
        popupWindow.setFocusable(false);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;   // 这里面拦截不到返回键
            }
        });
        // 如果希望showAsDropDown方法能够在下面空间不足时自动在anchorView的上面弹出
        // 必须在创建PopupWindow的时候指定高度，不能用wrap_content
        if(showStr.length==0) {
            if (left) {
                popupWindow.showAsDropDown(anchorView, anchorView.getRootView().getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 220), (int) (0 - anchorView.getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 50)));
            } else {
                popupWindow.showAsDropDown(anchorView, anchorView.findViewById(R.id.lv_center).getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 60), (int) (0 - anchorView.getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 50)));
            }
        }else if(showStr.length==3){
            if (left) {
                popupWindow.showAsDropDown(anchorView, anchorView.getRootView().getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 220), (int) (0 - anchorView.findViewById(R.id.ll_chatcontent).getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 62)));
            } else {
                popupWindow.showAsDropDown(anchorView, anchorView.findViewById(R.id.ll_chatcontent).getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 60), (int) (0 - anchorView.findViewById(R.id.ll_chatcontent).getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 62)));
            }
        }else if(showStr.length==1){
            if (left) {
                popupWindow.showAsDropDown(anchorView, anchorView.getRootView().getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 160), (int) (0 - anchorView.findViewById(R.id.ll_chatcontent).getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 62)));
            } else {
                popupWindow.showAsDropDown(anchorView, anchorView.findViewById(R.id.ll_chatcontent).getWidth() - (int) Utils.dp2px(anchorView.getContext().getResources(), 20), (int) (0 - anchorView.findViewById(R.id.ll_chatcontent).getHeight() - Utils.dp2px(anchorView.getContext().getResources(), 62)));
            }
        }
            return popupWindow;
    }

}
